package com.eden.door.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.eden.door.context.AppContext;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;

/**
 * 信息提示框
 * @author Eden
 *
 */
public class MessageBox extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private JButton okButton;
	private static MessageBox msgBox = null ;
	private static JFrame parent = null ;
	private JLabel message;
	private JPanel panel;
	
	/**
	 * 弹出模态对话框并展示信息
	 * @param msg
	 */
	public static void showMsgBox(String msg) {
		parent= AppContext.instance().getMainView().getFrame()  ;
		if(parent != null)
			msgBox = new MessageBox(parent);
		else
			msgBox = new MessageBox();
		
		msgBox.setMessage(msg) ; 
		
		msgBox.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		msgBox.setVisible(true);
	}
	
	/**
	 * 关闭模态显示框
	 */
	public static void closeMsgBox(){
		if(msgBox != null)
			msgBox.dispose() ;
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			MessageBox dialog ;
			JFrame parent = null ;
//			parent= AppContext.instance().getMainView().getFrame()  ;
			if(parent != null)
				dialog = new MessageBox(parent);
			else
				dialog = new MessageBox();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public MessageBox(JFrame parent) {
		super(parent , true ) ;
		init();
	}
	
	public MessageBox(){
		init();
	}
	
	
	/**
	 * 初始化界面
	 */
	public void init(){
		setTitle("提示信息") ;
		
		setIconImage(Toolkit.getDefaultToolkit().getImage("image\\ddd.png"));
		setBounds(100, 100, 487, 320);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			message = new JLabel("dsfsfsfs在票有肼顶耦老大哥地sdf");
			message.setFont(message.getFont().deriveFont(message.getFont().getSize() + 4f));
			message.setHorizontalTextPosition(SwingConstants.CENTER);
			message.setHorizontalAlignment(SwingConstants.CENTER);
			contentPanel.add(message);
		}
		{
			panel = new JPanel();
			panel.setBackground(new Color(0, 255, 204));
			panel.setPreferredSize(new Dimension(10, 30));
			contentPanel.add(panel, BorderLayout.NORTH);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(0, 255, 204));
			buttonPane.setPreferredSize(new Dimension(10, 55));
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("确定");
				okButton.setBackground(new Color(153, 255, 153));
				okButton.addActionListener(this);
				okButton.setPreferredSize(new Dimension(70, 30));
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.dispose() ;
	}

	public JLabel getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message.setText(message);
	}

}
